package com.it.vo;

import com.it.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/11/09/10:19
 * @注释: TODO
 **/
public class SecurityUser implements UserDetails {
    private final SysUser sysUser;

    private List<SimpleGrantedAuthority> simpleGrantedAuthorities;


    public SecurityUser (SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public void setSimpleGrantedAuthorities(List<SimpleGrantedAuthority> simpleGrantedAuthorities) {
        this.simpleGrantedAuthorities = simpleGrantedAuthorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities () {
        // todo 配置权限
        return simpleGrantedAuthorities;
    }

    @Override
    public String getPassword () {
        String myPassword = this.sysUser.getPassword();
        // 手动擦除密码防止传到前端
        this.sysUser.setPassword(null);
        return myPassword;
    }

    @Override
    public String getUsername () {
        return this.sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired () {
        return this.sysUser.getAccountNoExpired().equals(1);
    }

    @Override
    public boolean isAccountNonLocked () {
        return this.sysUser.getAccountNoLocked().equals(1);
    }

    @Override
    public boolean isCredentialsNonExpired () {
        return this.sysUser.getCredentialsNoExpired().equals(1);
    }

    @Override
    public boolean isEnabled () {
        return this.sysUser.getEnabled().equals(1);
    }
}
